define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'bonus/typebonus/index',
                    add_url: 'bonus/typebonus/add',
                    edit_url: 'bonus/typebonus/edit',
                    del_url: 'bonus/typebonus/del',
                    couponlist_url: 'bonus/userbonus/index',
                    coupon_url: 'bonus/typebonus/coupon',
                    multi_url: 'bonus/typebonus/multi',
                    table: 'bonus_type',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'type_id',
                sortName: 'type_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'type_id', title: __('Type_id')},
                        {field: 'type_name', title: __('Type_name')},
                        {field: 'type_money', title: __('Type_money'), operate:'BETWEEN'},
                        {field: 'send_text', title: __('发放数量'),operate: false},
                        {field: 'order_send_text', title: __('使用数量'),operate: false},
                        //sortable为是否排序,operate为搜索时的操作符,visible表示是否可见
                        {field: 'order_type', title: __('Order_type'),visible: false, searchList: {"0":__('Order_type 0'),"1":__('Order_type 1'),"2":__('Order_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'order_type_text', title: __('Order_type'),visible: false,operate: false,searchList: {"1":__('Order_type 1'),"2":__('Order_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'goods_type', title: __('Goods_type'),visible: false, searchList: {"0":__('Goods_type 0'),"1":__('Goods_type 1')}, formatter: Table.api.formatter.normal},
                        {field: 'goods_type_text', title: __('Goods_type'),visible: false,operate: false, searchList: {"0":__('Goods_type 0'),"1":__('Goods_type 1')}, formatter: Table.api.formatter.normal},
                        {field: 'send_type', title: __('Send_type'),visible: false, searchList: {"0":__('Send_type 0'),"1":__('Send_type 1'),"2":__('Send_type 2'),"3":__('Send_type 3'),"6":__('Send_type 6'),"7":__('Send_type 7'),"10":__('Send_type 10')}, formatter: Table.api.formatter.normal},
                        {field: 'send_type_text', title: __('Send_type'),visible: false,operate: false, searchList: {"0":__('Send_type 0'),"1":__('Send_type 1'),"2":__('Send_type 2'),"3":__('Send_type 3'),"6":__('Send_type 6'),"7":__('Send_type 7'),"10":__('Send_type 10')}, formatter: Table.api.formatter.normal},
                        {field: 'send_start_date', title: __('Send_start_date'),visible: false, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'send_end_date', title: __('Send_end_date'),visible: false, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'use_start_date', title: __('Use_start_date'),visible: false, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'use_end_date', title: __('Use_end_date'),visible: false, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'goods_id', title: __('Goods_id'),visible: false},
                        {field: 'day_time', title: __('Day_time'),visible: false, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,  formatter: Controller.api.formatter.operate}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        coupon:function(){
          $('#action').change(function () {
            if($('#action').val() == 2)
            {
              $('#userid').css('display','block');
              return false;
            }
            $('#userid').css('display','none');
            return false;
          });
          Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                // Form.api.bindevent($("form[role=form]"));
                Form.api.bindevent($("form[role=form]"),function (data,ret) {
                  console.log(ret);
                  if(ret.url)
                  {
                    window.open(ret.url);
                  }
                }, function(data, ret){

                });
            },
            formatter: {//渲染的方法
              operate: function (value, row, index) {
                var table = this.table;
                // 操作配置
                var options = table ? table.bootstrapTable('getOptions') : {};
                // 默认按钮组
                var buttons = $.extend([], this.buttons || []);
                if (options.extend.coupon_url !== ''){
                  buttons.push({
                    name: 'books',
                    icon: 'fa fa-bullhorn',
                    title: __('发放优惠券'),
                    text:'发放优惠券',
                    extend: 'data-toggle="tooltip"',
                    area:['50%', '80%'],
                    classname: 'btn btn-xs btn-success btn-coupon',
                    url: options.extend.coupon_url,
                  });
                }
                if (options.extend.couponlist_url !== ''){
                  buttons.push({
                    name: 'books',
                    icon: 'fa fa-bullseye',
                    title: __('查看列表'),
                    text:'查看列表',
                    extend: 'data-toggle="tooltip"',
                    area:'["100%","100%"]',
                    classname: 'btn btn-xs btn-primary btn-couponlist',
                    url: options.extend.couponlist_url,
                  });
                }
                if (options.extend.edit_url !== ''){
                  buttons.push({
                    name: 'edit',
                    icon: 'fa fa-pencil',
                    title: __('Edit'),
                    text:__('Edit'),
                    extend: 'data-toggle="tooltip"',
                    classname: 'btn btn-xs btn-success btn-editone',
                    url: options.extend.edit_url
                  });
                }
                // if (options.extend.del_url !== '') {
                //   buttons.push({
                //     name: 'del',
                //     icon: 'fa fa-trash',
                //     title: __('Del'),
                //     extend: 'data-toggle="tooltip"',
                //     classname: 'btn btn-xs btn-danger btn-delone',
                //     url: options.extend.del_url
                //   });
                // }
                return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
              },
            }

        }
    };
    return Controller;
});